package com.yonyou.pmclouds.project.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.BuziExCodeConst;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.mapper.ProjectMapper;
import org.apache.commons.lang3.StringUtils;

public class ProjectUniqueCheck implements SingleProcessor<ProjectVO> {
    private ProjectMapper mapper;

    public ProjectUniqueCheck(ProjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void process(ProjectVO vo, ProjectVO orig) throws BusinessException {
        int codeCount;
        // 从NC同步过来的项目
        if (StringUtils.isNotEmpty(vo.getPkSrc())) {
            codeCount = mapper.countCodeOfOrg(vo.getPkTenant(), vo.getScode(), vo.getPkOrg());
        } else {
            codeCount = mapper.countWithCode(vo);
        }
        boolean codeNotUnique = true;
        if (codeCount == 0) {
            codeNotUnique = false;
        }

//        boolean nameNotUnique = true;
//        int nameCount = mapper.countWithName(vo);
//        if (nameCount == 0) {
//            nameNotUnique = false;
//        }
//        if (codeNotUnique && nameNotUnique) {
//            throw new BusinessException("已经存在相同的项目编码，请修改!", BuziExCodeConst.CODE_NAME_CODE_DUPLICATE);
//
//        }
        if (codeNotUnique) {
            throw new BusinessException("已经存在相同的项目编码，请修改!", BuziExCodeConst.CODE_CODE_DUPLICATE);
        }
//        if (nameNotUnique) {
//            throw new BusinessException("已经存在相同的项目名称，请修改!", BuziExCodeConst.CODE_NAME_DUPLICATE);
//        }
    }
}

